package com.tang.wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 读取socket文本流，无界流
 * 并行度的优先级：
 * 代码：算子 > 代码：env > 提交时指定 > 配置文件
 *
 * @author tang
 * @since 2023/5/29 12:09
 */
public class WordCountStreamUnboundedDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // IDEA运行时，也可以看到webui，一般用于本地测试
        // 需要引入一个依赖 flink-runtime-web
        // 在idea运行，不指定并行度，默认就是 电脑的 线程数
        // StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        //env.setParallelism(3); // 在ENV设置的总算子

        DataStreamSource<String> socketDateStreamSource = env.socketTextStream("192.168.70.141", 7777);

        SingleOutputStreamOperator<Tuple2<String, Integer>> sum = socketDateStreamSource
                .flatMap((FlatMapFunction<String, Tuple2<String, Integer>>) (value, out) -> {
                    for (String word : value.split(" ")) {
                        out.collect(Tuple2.of(word, 1));
                    }
                })//.setParallelism(2) // 单独设置的算子
                .returns(Types.TUPLE(Types.STRING, Types.INT)) // 这个方法的意思是，指定返回值
                .returns(new TypeHint<Tuple2<String, Integer>>() {})
                .keyBy(value -> value.f0)
                .sum(1);
        sum.print();
        env.execute();
    }

}
